package org.ovgu.coursify;

import org.ovgu.coursify.data.DataBase;
import org.ovgu.coursify.data.Lecture;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RatingBar;
import android.widget.TextView;

public class CommentAndRateActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_comment_and_rate);
		Lecture lecture = DataBase.getLection(HomeActivity.SELECTEDCOURSE);
		((TextView) findViewById(R.id.commentLabel)).setText("Comment on "
				+ lecture.getName() + ":");

		((Button) findViewById(R.id.submitButton))
				.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						pushComment();
						openCommentars();
					}
				});

	}

	protected void pushComment() {
		EditText edit = (EditText) findViewById(R.id.editText1);
		RatingBar rating = (RatingBar) findViewById(R.id.ratingBar1);

		DataBase.addComment(HomeActivity.SELECTEDCOURSE, edit.getText()
				.toString(), rating.getRating());

		DataBase.saveComments();
	}

	private void openCommentars() {
		Intent ide = new Intent(CommentAndRateActivity.this, ViewComments.class);
		ide.putExtra("org.ovgu.coursify.viewComments", "ViEWCOMMENT");
		ide.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		startActivity(ide);
	}
}
